﻿@page "/finishgoodscounts"
@inject NavigationManager NavigationManager
@inject IFinishGoodsCountService FinishGoodsCountService

<PageTitle>FS成品库存</PageTitle>
<!--sticky固定位置，top: 3.5rem-->
<div style="position: sticky; top: 3.5rem; z-index: 1;background-color:White">
    <div class="row position-relative">
        <div class="col input-group mb-2">
            <span class="input-group-text">搜索</span>
            <input class="form-control" placeholder="Search"
                   @bind-value="@_search" @oninput="@Search" />
        </div>
        <div class="col form-check ms-3 mt-2">
            <input class="form-check-input" type="checkbox" value="@_showAll" @onchange="OnShowAllChange">
            <label class="form-check-label">
                显示全部（当年已发货项目）
            </label>

        </div>
        <div class="col mt-2">
            <label>总货物面积（不包含外仓）@_totalArea 平方米，堆叠后 @(_totalArea/2d) 平方米。</label>
        </div>
        
        @* <CompAddButton Roles="admin,pmc,pm" Url="mainplan/add"/> *@
    </div>
</div>

<table class="table table-hover table-striped" style="table-layout: fixed; word-break: break-all;">
    <!--sticky固定位置，top: 6rem-->
    <thead style="position: sticky; top: 6rem; z-index: 1; background-color: White">
        <tr>

            <th style="width: 120px">订单号</th>
            <th style="width: 70px; text-align: center">BU</th>
            <th>项目名称</th>
            <th style="width: 7%; text-align: center">托盘数量</th>
            <th style="width: 7%; text-align: center">货物面积</th>
            <th style="width: 7%; text-align: center">入库日期</th>
            <th style="width: 7%; text-align: center">合同提货日期</th>
            <th style="width: 7%; text-align: center">出库日期</th>
            <th style="width: 7%; text-align: center">库龄(Day)</th>
            <th style="width: 7%; text-align: center">预估仓储费</th>
            <th style="width: 7%; text-align: center">移至外仓日期</th>
            <th style="width: 7%; text-align: center">外仓货物面积</th>
            <th style="width: 7%; text-align: center">库位</th>
        </tr>
    </thead>
    <tbody>
        @if (_filterFinishedGoodsCountDtos.Count == 0)
        {
            <p>...</p>
        }
        else
        {
            @foreach (var dto in _filterFinishedGoodsCountDtos)
            {
                <tr class="@(dto.Partially ? "table-warning" : "")">
                    <td>
                        @if (dto.MainPlanId == null)
                        {
                            @dto.OdpNumber
                        }
                        else
                        {
                            <a href="pallets/mainplan/@dto.MainPlanId">@dto.OdpNumber</a>
                        }
                    </td>
                    <td style="text-align: center">@dto.Sbu</td>
                    <td style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap">@dto.Name</td>
                    <td style="text-align: center">@dto.PalletQuantity</td>
                    <td style="text-align: center">@dto.Area</td>
                    <td style="text-align: center">
                        @(dto.WarehousingTime != null ?
                            dto.WarehousingTime.Value.ToString("MM/dd") : "")
                    </td>
                    <td style="text-align: center">@dto.FinishTime.ToString("MM/dd") </td>
                    <td style="text-align: center">
                        @(dto.ShippingTime != null ?
                            dto.ShippingTime.Value.ToString("MM/dd") : "")
                    </td>
                    <td style="text-align: center">@dto.Aging</td>
                    <td style="text-align: center">@dto.StorageFee</td>
                    <td style="text-align: center">
                        @(dto.OutsideTime != null ?
                            dto.OutsideTime.Value.ToString("MM/dd") : "")
                    </td>
                    <td style="text-align: center">@dto.OutsideArea</td>
                    <td style="text-align: center">@dto.Location</td>
                </tr>
            }
        }
    </tbody>
</table>

@code {
    //过滤参数
    private string _search = string.Empty;
    private bool _showAll;
    private double _totalArea;
    private List<FinishedGoodsCountDto> _finishedGoodsCountDtos = new();
    private List<FinishedGoodsCountDto> _filterFinishedGoodsCountDtos = new();

    protected override async Task OnParametersSetAsync()
    {
        await GetData();
        Search(new ChangeEventArgs { Value = "" });
        StateHasChanged();
    }

    private async void OnShowAllChange(ChangeEventArgs e)
    {
        _showAll =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }

    private async Task GetData()
    {
        _finishedGoodsCountDtos.Clear();
        _finishedGoodsCountDtos.AddRange(await FinishGoodsCountService.GetAllWithFeeAsync(_showAll));
        //异步方法需要主动告知前台状态已经发生改变
        StateHasChanged();
    }

    //过滤逻辑
    private void Search(ChangeEventArgs e)
    {
        //将搜索放在外面做（4.没搜索||5.搜索项目号||6.搜索项目名称）
        _search=e.Value!.ToString()!;
        _filterFinishedGoodsCountDtos.Clear();
        _filterFinishedGoodsCountDtos.AddRange(_finishedGoodsCountDtos.Where(
            x => string.IsNullOrEmpty(_search) ||
                 x.OdpNumber.ToLower().Contains(_search.ToLower()) ||
                 x.Name.ToLower().Contains(_search.ToLower())));
        _totalArea = _filterFinishedGoodsCountDtos.Sum(x => x.Area);
        StateHasChanged();
    }


}
